#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File  : __init__.py
@Author: different && 道阻且长,行则将至 行而不辍,未来可期
@Date  : 2021/08/03
@Desc  : 工厂函数

"""
import os

from flask import Flask
from flask_caching import Cache
from flask_login import LoginManager
from flask_migrate import Migrate
from redis import Redis

import settings
from expand import db
from settings import setting

login_manager = LoginManager()
migrate = Migrate()
redis = Redis()
cache = Cache()


def create_app():
    app = Flask(__name__, template_folder='../templates', static_folder='../static')

    # 加载配置文件
    app.config.from_object(setting[os.environ.get('ENV') if os.environ.get('ENV') else 'production'])

    # 设置文件上传
    app.config['UPLOAD_FOLDER'] = settings.UPLOAD_FOLDER

    # 扩展应用初始化
    cache.init_app(app)
    db.init_app(app)
    migrate.init_app(app, db)
    login_manager.init_app(app)

    # 导入蓝图
    from apps.view.charts import charts_bp
    from apps.view.index import index_bp
    from apps.view.upload import upload_bp
    from apps.view.user import user_bp
    from apps.view.sheet import sheet_bp
    from apps.view.cross import cross_bp

    # 加载蓝图
    app.register_blueprint(index_bp)
    app.register_blueprint(upload_bp)
    app.register_blueprint(charts_bp)
    app.register_blueprint(user_bp)
    app.register_blueprint(sheet_bp)
    app.register_blueprint(cross_bp)
    return app
